<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['status']=$this->xieyi_status;
$return['type']=array('0'=>'单方签署','1'=>'双方签署');
$return['time']=array('0'=>'一次','1'=>'多次');
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=trim($_RQ['status']);
		$return['params']['status']=intval($_RQ['status']);
	}
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_aihire_hetongtpl',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$item){
		$item['merchtitle']=pdo_getcolumn('jy_aihire_merch',array('id'=>$item['merchid']),'title');
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'个人合同模板'),
			array('op'=>'edit','title'=>'添加合同模板'),
		),
		'list'=>array('data'=>'list','list'=>array(	
			array('name'=>'title','title'=>'合同模版名称','type'=>'string'),
			array('name'=>'type','title'=>'模板类型','type'=>'radio','options'=>'type'),
			array('name'=>'merchtitle','title'=>'签章法人实体','type'=>'string'),
			array('name'=>'time','title'=>'每人签署次数','type'=>'radio','options'=>'time'),
			//array('name'=>'workerid','title'=>'创建人','type'=>'select','options'=>'workers'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),		
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),	
		)),
		'search'=>array(
			array('name'=>'title','width'=>10,'title'=>'合同名称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
$steps=array('基本信息','合同DIY');
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_aihire_hetongtpl',array('id'=>$id));
		$item['oldpdf']=$item['pdf'];
	}else{
		$item=array();
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'合同设置',
		'submit_title'=>'保存并下一步',
		'tabs'=>array(
			array('op'=>'list','title'=>'合同管理'),
			array('op'=>'edit','title'=>'添加合同')
		),
		'edit'=>array(
			array('name'=>'title','title'=>'合同模版名称','type'=>'string'),
			array('name'=>'type','title'=>'模板类型','type'=>'radio','options'=>'type'),
			array('name'=>'time','title'=>'每人签署次数','type'=>'radio','options'=>'time'),
			array('name'=>'pdf','title'=>'导入合同文件','type'=>'upfile','help'=>'1.合同范本格式仅支持PDF，上传PDF文件为最佳；2.文件大小≤20Mb；'),			
		)
	);
	$return['htmls'][]=array('type'=>'steps','steps'=>$steps,'step'=>'1');
	$return['htmls'][]=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['merchid']){
		//exi('请选择签章法人实体！','error');
	}
	if(!$postdata['id'] && !$postdata['pdf']){
		exi('请导入合同文件！','error');
	}
	$data=array_fields(array('title','pdf','type','merchid','time'),$postdata);
	if($postdata['pdf'] && $postdata['pdf']!=$postdata['oldpdf']){
		$imgs=$postdata['images'];
		$data['images']=$this->model('model')->pdf2png($postdata['pdf'],'',$imgs);
		$data['images']=ijson_encode($data['images']);
	}
	$id=intval($postdata['id']);
	if($id){		
		pdo_update('jy_aihire_hetongtpl',$data,array('id'=>$id));
		exi('','','/merch.hetongtpl/diy/id='.$id);
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$merchid;
		$data['createtime']=time();
		pdo_insert('jy_aihire_hetongtpl',$data);
		exi('','','/merch.hetongtpl/diy/id='.$id);
	}
}
if($_RQ['op']=='diy'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_aihire_hetongtpl',array('id'=>$id));
	}else{
		$item=array();
	}
	$item['diys']=$item['diys']?json_decode($item['diys'],true):array();
	$item['images']=$item['images']?json_decode($item['images'],true):array('../addons/jy_aihire/resource/images/demotpl0.png','../addons/jy_aihire/resource/images/demotpl.png');
    $return['zujian1']=array(
		array('type'=>0,'title'=>'单行文本','width'=>131,'height'=>36),
		array('type'=>1,'title'=>'多行文本','width'=>131,'height'=>54),
		array('type'=>2,'title'=>'个人签章位置','width'=>130,'height'=>70),
		array('type'=>3,'title'=>'企业签章位置','width'=>160,'height'=>160),
		array('type'=>4,'title'=>'骑缝章','width'=>120,'height'=>160),
	);
	$return['zujian2']=array(
		array('type'=>0,'title'=>'甲方公司名称','name'=>'jfname','read'=>true,'width'=>236,'height'=>36),
		array('type'=>0,'title'=>'签署日期','name'=>'jfqianday','read'=>true,'width'=>236,'height'=>36),
		array('type'=>0,'title'=>'任务名称','name'=>'jfjobtitle','read'=>true,'width'=>236,'height'=>36),
		array('type'=>0,'title'=>'任务开始日期','name'=>'jfjobstart','read'=>true,'width'=>236,'height'=>36),
		array('type'=>0,'title'=>'任务结束日期','name'=>'jfjobend','read'=>true,'width'=>236,'height'=>36),
		array('type'=>0,'title'=>'关联客户','name'=>'jfglkh','read'=>true,'width'=>236,'height'=>36),
		array('type'=>0,'title'=>'今日-结束时间（一年）','name'=>'jfjinend','read'=>true,'width'=>236,'height'=>36),
		array('type'=>0,'title'=>'昨日-结束时间（一年）','name'=>'jfzuoend','read'=>true,'width'=>236,'height'=>36),
		array('type'=>0,'title'=>'明日-结束时间（一年）','name'=>'jfmingend','read'=>true,'width'=>236,'height'=>36),
	);
	$return['zujian3']=array(
		array('type'=>0,'title'=>'姓 名','name'=>'yfname','read'=>true,'width'=>236,'height'=>36),
		array('type'=>0,'title'=>'联系地址','name'=>'yfaddress','read'=>true,'width'=>236,'height'=>36),
		array('type'=>0,'title'=>'证件号码','name'=>'yfcodesn','read'=>true,'width'=>236,'height'=>36),
		array('type'=>0,'title'=>'联系电话','name'=>'yfmobile','read'=>true,'width'=>236,'height'=>36),
		array('type'=>0,'title'=>'户籍所在地','name'=>'yfhuji','read'=>true,'width'=>236,'height'=>36),
		array('type'=>0,'title'=>'签约日期','name'=>'yfqianday','read'=>true,'width'=>236,'height'=>36),
		array('type'=>0,'title'=>'银行名称','name'=>'yfbanktitle','read'=>true,'width'=>236,'height'=>36),
		array('type'=>0,'title'=>'银行卡号','name'=>'yfbanksn','read'=>true,'width'=>236,'height'=>36),
		array('type'=>0,'title'=>'开户行','name'=>'yfbankname','read'=>true,'width'=>236,'height'=>36),
	);
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='postdiy'){
	$postdata=get_postdata();
	$data['diys']=ijson_encode($postdata['diys']);
	$id=intval($postdata['id']);
	if($id){		
		pdo_update('jy_aihire_hetongtpl',$data,array('id'=>$id));
		exi('提交成功','','/merch.hetongtpl/list/');
	}else{
		exi('提交异常','error','/merch.hetongtpl/list/');
	}
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['status'])){
		$update['status']=intval($post['status']);
	}
	if($update){
		pdo_update('jy_aihire_hetongtpl',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_aihire_hetongtpl',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_aihire_hetongtpl',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>